<script src="/static/jquery-3.2.1.min.js">
    window.onunload(alert("jin3"))
</script>
<html>
<head>
    <title>会员卡列表</title>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" href="/static/table/bootstrap-table.min.css"/>
    <script type="text/javascript" src="/static/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/table/bootstrap-table.js"></script>
    <script type="text/javascript" src="/static/table/locale/bootstrap-table-zh-CN.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/static/sweetalert/sweetalert.css"/>
    <script type="text/javascript" src="/static/sweetalert/sweetalert.min.js"></script>

    <link rel="stylesheet" href="/static/date/bootstrap-datetimepicker.min.css"/>
    <script type="text/javascript" src="/static/date/Moment.js"></script>
    <script type="text/javascript" src="/static/date/bootstrap-datetimepicker.min.js"></script>

    <script>
        $(function () {

            //异步加载教师下拉框
            $.getJSON("/coach/list", {}, function (releset) {
                var e = releset.data;
                $(e).each(function () {
                    $('#coachid').append('<option value=' + this.coachName + ' >' + this.coachName + '</option>');
                })
            });

            //异步加载课程下拉框
            $.getJSON("/subject/list", {}, function (releset) {
                var e = releset.data;
                $(e).each(function () {
                    $('#subjectid').append('<option value=' + this.subname + ' >' + this.subname + '</option>');
                })
            });

            //课程下拉框选中
            $("#subject").change(function () {
                var sid = $("#subject").val();
                if (sid != -1) {
                    $.getJSON("/subject/queryById/" + sid, {}, function (e) {
                        $('#dj').val(e.data.sellingPrice);
                        var s = 1;
                        $('#sl').val(s);
                        $('#zje').val(e.data.sellingPrice * s);
                    })
                } else {
                    alert("请选中课程!");
                }
            });

            /*$('#sl').change(function () {
            var sl = $('#sl').val();
            var dj = $('#dj').val();
            var s = sl * dj;
            $('#zje').val(s);
        })
            $('#ssq').change(function () {
                var ssq = $('#ssq').val();
                var zje = $('#zje').val();
                var s = ssq-zje;
                $('#zl').val(s);
            })*/

            $('#table').bootstrapTable({
                url: '/privatecoachinfo/queryPage',
                method: 'get',
                contentType: "application/x-www-form-urlencoded",

                columns: [{
                    field: 'pid',
                    title: '编号'
                },
                    {
                        field: 'member.memberName',
                        title: '学员名称'
                    },
                    {
                        field: 'coach.coachName',
                        title: '教师名称'
                    }, {
                        field: 'subject.subname',
                        title: '课程名称'
                    },
                    {
                        field: 'count',
                        title: '数量',
                    },
                    {
                        field: 'xx', title: '操作',
                        formatter: function (value, row, index) {
                            return "<a title='删除' href='javascript:del1("
                                + row.pid + ")'><span class='glyphicon glyphicon-trash'></span></a> | <a href='javascript:upd(" + row.pid + ")' class='glyphicon glyphicon-pencil'></a>";

                        }
                    }
                ],
                queryParamsType: '',
                queryParams: queryParams,
                height: 360,
                pageList: [5, 10, 15],
                pageNumber: 1,
                pageSize: 5,
                pagination: true,
                sidePagination: 'server',
                detailView: true,
                onExpandRow: function (index, row, $detail) {
                    var aa = $detail.html("<table></table>").find('table');
                    var idd = row.pid;
                    aa.bootstrapTable({
                        url: '/privatecoachinfo/getDetailsById/' + idd,
                        method: 'get',
                        columns: [{
                            field: 'pid',
                            title: '编号',
                        },
                            {
                                field: 'member.memberName',
                                title: 'vip学员名称'
                            },
                            {
                                field: 'count',
                                title: '数量'
                            }, {
                                field: 'state',
                                title: '状态',
                                formatter: function (value, row, index) {
                                    if (row.state == 1) {
                                        return "购买";
                                    } else {
                                        return "赠送";
                                    }
                                }
                            }, {
                                field: 'countprice',
                                title: '金额',
                            },
                            {
                                field: 'remark',
                                title: '备注',

                            }, {
                                field: 'date',
                                title: '开始日期',
                            }
                        ]
                    });
                },
            })
        });

        //新增时候的放大镜
        function kan1() {
            $("#myModal3").modal("show");
            $('#tb').bootstrapTable({
                url: '/member/list',
                method: 'get',
                contentType: "application/x-www-form-urlencoded",
                columns: [{
                    radio: true
                }, {
                    field: 'memberId',
                    title: '编号'
                }, {
                    field: 'memberName',
                    title: 'vip学员名称'
                }, {
                    field: 'membertypes.typeName',
                    title: 'vip学员类型'
                }
                ],
                queryParamsType: '',
                queryParams: queryParams,
                height: 360,
                pagination: true,
                onDblClickRow: function (row) {
                    $('#myModal3').modal("hide");
                    id = row.memberId;
                    $.getJSON("/member/queryById/" + id, {}, function (result) {
                        var e = result.data;

                        //查询私教次数
                        $.getJSON("/privatecoachinfo/count", {'memid': id}, function (e) {
                            $('#cishu').text(e.data);
                        });

                        $('#kh').val(e.memberId);
                        $('#xm').text(e.memberName);
                        $('#type').text(e.membertype.typeName);
                        $('#dqdate').text(e.memberxufei);
                        $('#hystatic').text(e.memberStatic);

                        if (e.memberStatic == 1) {
                            $('#hystatic').text("正常");
                        } else {
                            $('#hystatic').text("请续费");
                        }
                    })
                }
            })
        }

        //放大镜
        function kan2() {
            $("#myModal3").modal("show");
            $('#tb').bootstrapTable({
                url: '/member/list',
                method: 'get',
                contentType: "application/x-www-form-urlencoded",
                columns: [{
                    radio: true
                }, {
                    field: 'memberId',
                    title: '编号'
                }, {
                    field: 'memberName',
                    title: 'vip学员名称'
                }, {
                    field: 'membertype.typeName',
                    title: 'vip学员类型'
                }
                ],
                queryParamsType: '',
                queryParams: queryParams,
                height: 360,
                pagination: true,
                onDblClickRow: function (row) {
                    $('#myModal3').modal("hide");
                    id = row.memberId;
                    $.getJSON("/member/queryById/" + id, {}, function (e) {
                        $('#memberid').val(e.data.memberId);
                    })
                }
            })
        }

        //删除
        function del1(id) {
            swal({
                    title: "确定删除吗？",
                    text: "您将无法恢复！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定删除！",
                    cancelButtonText: "取消删除！",
                    closeOnConfirm: false,
                    closeOnCancel: false
                },
                function (isConfirm) {
                    if (isConfirm) {
                        var opt = $('#table').bootstrapTable('getOptions');
                        var coachid = $('#coachid').val();
                        var subjectid = $('#subjectid').val();
                        var memberid = $('#memberid').val();
                        $.post('/privatecoachinfo/delete/'+id, {
                            '_method': 'delete'
                        }, function (result) {
                            if(result.code==200){
                                //重新给table绑定数据
                                $("#table").bootstrapTable("load", result);
                                swal("删除！", "删除成功", "success");
                                search();
                            }else{
                                //重新给table绑定数据
                                $("#table").bootstrapTable("load", result);
                                swal("删除！", "删除异常", "success");
                                search();
                            }
                        });

                    } else {
                        swal("取消！", "您已取消删除)",
                            "error");
                    }
                });
        }

        //获取当前的条件个页面页数即使更新值
        function queryParams(afds) {
            var i = {
                "pageSize": afds.pageSize,
                "pageNumber": afds.pageNumber,
                "coachName": $('#coachid').val(),
                "subname": $('#subjectid').val(),
                "memberid": $('#memberid').val(),
            };
            return i;
        }

        //查询
        function search() {
            var opt = $('#table').bootstrapTable('getOptions');
            var coachName = $('#coachid').val();
            var subname = $('#subjectid').val();
            var memberid = $('#memberid').val();
            $.getJSON("/privatecoachinfo/queryPage", {
                "pageSize": opt.pageSize,
                "pageNumber": 1,
                "coachName": coachName,
                "subname": subname,
                "memberid": memberid
            }, function (releset) {
                $("#table").bootstrapTable('load', releset);
            })
        }

        //下拉框赋值
        function totian() {
            $("#kh").val("");

            $("#coach").html("");
            $("#subject").html("");

            $("#datett").val("");

            $("#bz").val("");
            $("#sl").val("");
            $('#zje').val("");
            $('#ssq').val("");
            $('#cishu').text("");
            $('#kh').val("");
            $('#xm').text("");
            $('#type').text("");
            $('#dqdate').text("");
            $('#hystatic').text("");

            //异步加载教师下拉框
            $.getJSON("/coach/list", {}, function (releset) {
                var e = releset.data;
                $('#coach').append('<option value="-1">--请选中教师--</option>');
                $(e).each(function () {
                    $('#coach').append('<option value=' + this.coachId + ' >' + this.coachName + '</option>');
                })
            });

            //异步加载课程下拉框
            $.getJSON("/subject/list", {}, function (releset) {
                var e = releset.data;
                $('#subject').append('<option value="-1">--请选中课程--</option>');
                $(e).each(function () {
                    $('#subject').append('<option value=' + this.subId + ' >' + this.subname + '</option>');
                })
            });

            // $.post("/private/topcoach",{},function (releset) {
            //     var e=releset;
            //     var ss ="";
            //     var ssss="";
            //     var sss="<option value='-1'>"+"--请选择--"+"</option>";
            //     $(e.coach).each(function () {
            //         /*$('#coach').append('<option value='+this.coachId+' >'+this.coachName+'</option>');*/
            //         ss += "<option value='"+this.coachId+"'>"+"❤"+this.coachName+"</option>";
            //         ssss=sss+ss;
            //         $('#coach').html(ssss);
            //     });
            //
            //     var tt ="";
            //     var tttt="";
            //     var ttt="<option value='-1'>"+"--请选择--"+"</option>";
            //     $(e.subject).each(function () {
            //         /* $('#subject').append('<option value='+this.subId+' >'+this.subname+'</option>');*/
            //         tt += "<option value='"+this.subId+"'>"+"☆☆☆"+this.subname+"</option>";
            //         tttt=ttt+tt;
            //         $('#subject').html(tttt);
            //     })
            // });

        }


        //添加
        function save() {
            if (!validateAdd()) {
                return;
            }
            //接收数据
            var opt = $('#table').bootstrapTable('getOptions');
            var coachid = $('#coachid').val();
            var subjectid = $('#subjectid').val();
            var memberid = $('#memberid').val();
            var hyid = $("#kh").val();
            var jlid = $("#coach").val();
            var kcid = $("#subject").val();
            var date = $("#datett").val();
            var state = 1;
            var remark = $("#bz").val();
            var count = $('#sl').val();
            var countprice = $("#zje").val();
            var realprice = $('#ssq').val();
            $("#myModal").modal("hide");

            $.getJSON('/coach/queryById/' + jlid, {}, function (result) {
                $("#table").bootstrapTable("load", result);
                if (result.data.coachStatic == 0) {
                    $.getJSON("/member/queryById/" + hyid, {}, function (res) {
                        if (res.data.memberbalance >= countprice) {
                            $.post('/privatecoachinfo/add', {
                                'memberid': hyid,
                                'coachid': jlid,
                                'subjectid': kcid,
                                'count': count,
                                'countprice': countprice,
                                'date': date,
                                'state': state,
                                "remark": remark
                            }, function (result) {
                                $("#table").bootstrapTable("load", result);
                                if (result.code == 200) {
                                    swal("添加！", "添加会员课程成功", "success");
                                    search();
                                } else {
                                    swal("失败！", "添加会员课程异常", "error");
                                    search();
                                }
                            });
                        } else {
                            swal("失败！", "余额不足！", "error");
                            search();
                        }
                    });

                } else {
                    swal("失败！", "此教师正休假或已离职", "error");
                    this.search();
                }

            })
        }

        function validateAdd() {
            $("#kh").parent().find("span").remove();
            $("#coach").parent().find("span").remove();
            $("#datett").parent().find("span").remove();
            $("#subject").parent().find("span").remove();
            $("#sl").parent().find("span").remove();
            $("#ssq").parent().find("span").remove();


            var kh = $("#kh").val().trim();
            if (kh == null || kh == "") {
                $("#kh").parent().append("<span style='color:red'>请选择会员卡号</span>");
                return false;
            }

            var coach = $("#coach").val().trim();
            if (coach == -1) {
                $("#coach").parent().append("<span style='color:red'>请选择教师姓名</span>");
                return false;
            }

            var datett = $("#datett").val().trim();
            if (datett == null || datett == "") {
                $("#datett").parent().append("<span style='color:red'>请选择购买日期</span>");
                return false;
            }

            var subject = $("#subject").val().trim();
            if (subject == -1) {
                $("#subject").parent().append("<span style='color:red'>请选择项目名称</span>");
                return false;
            }


            var sl = $("#sl").val().trim();
            if (sl == null || sl == "") {
                $("#sl").parent().append("<span style='color:red'>请填写数量</span>");
                return false;
            }

            if (!(/^[1-9]\d*$/.test(sl))) {
                $("#sl").parent().append("<span style='color:red'>须为正整数</span>");
                return false;
            }

            return true;
        }


        //修改赋值
        function upd(id) {
            $("#myModal2").modal("show");
            $('#id').val(id);
            $.getJSON('/privatecoachinfo/getDetailsById/' + id, {}, function (result) {
                //$("#table").bootstrapTable("load",result.data) ;
                console.log(JSON.stringify(result[0]));

                var coachId = result[0].coach.coachId;

                $("#hykh").text(result[0].member.memberId);
                $("#hyxm").text(result[0].member.memberName);
                $("#yssj").text(result[0].coach.coachName);

                $.getJSON("/coach/availableList", {}, function (releset) {
                    var e = releset.data;
                    var tt = "";
                    var tttt = "";
                    var ttt = "<option value='-1'>" + "--请选择--" + "</option>";
                    $(e).each(function () {
                        if (this.coachId != coachId) {
                            tt += "<option value='" + this.coachId + "'>" + "❤" + this.coachName + "</option>";
                            tttt = ttt + tt;
                        }
                    });
                    $('#xgcoach').html(tttt);
                })
            });
        }

        //修改
        function upd1() {
            if (!validateUpd()) {
                return;
            }
            var opt = $('#table').bootstrapTable('getOptions');
            var coachid = $('#coachid').val();
            var subjectid = $('#subjectid').val();
            var memberid = $('#memberid').val();
            var id = $('#id').val();
            var coachname = $("#xgcoach").val();
            $("#myModal2").modal("hide");

            $.post('/privatecoachinfo/update', {'_method': 'put', 'pid': id, 'coachid': coachname}, function (result) {
                if(result.code==200){
                    $("#table").bootstrapTable("load", result);
                    swal("修改！", "修改成功", "success");
                    search();
                }else{
                    swal("修改！", "修改失败", "error");
                    search();
                }
            });
        }


        function validateUpd() {
            $("#xgcoach").parent().find("span").remove();
            var xgcoach = $("#xgcoach").val().trim();
            if (xgcoach == -1) {
                $("#xgcoach").parent().append("<span style='color:red'>请选择变更老师</span>");
                return false;
            }
            return true;
        }


        function zql() {
            var jine = $('#zje').val();
            var ssjine = $('#ssq').val();
            var zhao = ssjine - jine;
            $('#zl').val(zhao);
        }

        function slChange() {
            var sl = $('#sl').val();
            var dj = $('#dj').val();
            var s = sl * dj;
            $('#zje').val(s);
        }

    </script>
</head>
<body background="/static/HTmoban/images/tongji4.jpg">

<div class="panel panel-default">
    <div class="panel-body">
        <form class="form-inline">
            <div class="form-group">
                会员编号：<input id="memberid" type="text" class="form-control" placeholder="请输入会员编号">
                <a title='查询' onclick="kan2()" href="#"><span class='glyphicon glyphicon-search'></span></a>
            </div>
            &nbsp;&nbsp;&nbsp;
            老师名称:
            <select id="coachid" class="form-control">
                <option value="">请选择</option>
            </select>
            &nbsp;&nbsp;&nbsp;
            课程名称:
            <select id="subjectid" class="form-control">
                <option value="">请选择</option>
            </select>

            <button onclick="search()" type="button" class="btn btn-default">查询</button>
            <button type="button" class="btn btn-default" style="float: right;" data-toggle="modal"
                    data-target="#myModal" onclick="totian()"><span class="glyphicon glyphicon-plus"></span>添加会员私教课程
            </button>

        </form>
    </div>
</div>

<div>
    <table id="table"></table>
</div>
<!--新增模态框-->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">❤增加新老师信息</h4>
            </div>
            <div class="modal-body">
                <!-- form开始 -->
                <form>
                    <label>会员信息</label>
                    <table style="margin-top:10px;border:1px solid #ccc;padding:20px;margin-bottom:20px;">
                        <tr style="margin: 20px; margin-left: 20px;padding: 20px;padding-left: 20px">
                            <td style="width:450px;padding-left: 20px"><label
                                    style="float: left;margin-top: 10px">会员卡号：</label><input type="text" id="kh"
                                                                                             style="margin-top: 10px;width: 250px"
                                                                                             class="form-control"
                                                                                             readonly="readonly">
                                <a title='查询' onclick="kan1()" href="#"
                                   style="float: right;margin-top: -30px;margin-right: 20px"
                                   class='glyphicon glyphicon-search'></a>
                            </td>
                            <td style="width:300px"><label style="float: left;margin-top: 10px">会员姓名：</label><span
                                    style="display: inline-block;margin-top: 10px" id="xm"></span></td>
                            <td style="width:300px"><label style="float: left;margin-top: 10px">会员类型：</label><span
                                    style="display: inline-block;margin-top: 10px" id="type"></span></td>
                        </tr>
                        <tr style="margin-top: 20px; margin-left: 20px;padding: 20px;height: 40px;padding-left: 20px">
                            <td style="width:450px;padding-left: 20px"><label
                                    style="float: left;margin-top: 10px">到期时间：</label><span
                                    style="display: inline-block;margin-top: 10px" id="dqdate"></span></td>
                            <td style="width:300px"><label style="float: left;margin-top: 10px">私教次数：</label><span
                                    style="display: inline-block;margin-top: 10px" id="cishu"></span></td>
                            <td style="width:300px"><label style="float: left;margin-top: 10px">会员状态：</label><span
                                    style="display: inline-block;margin-top: 10px" id="hystatic"></span></td>
                        </tr>
                    </table>
                    <label>购买私教</label>
                    <table style="margin-top:10px;border:1px solid #ccc;padding:20px;margin-bottom:20px;">
                        <tr style="margin: 20px; margin-left: 20px;padding: 20px;">
                            <td style="width:450px;padding-left: 20px"><label
                                    style="float: left;margin-top: 10px">教师姓名：</label>
                                <select id="coach" style="margin-top:10px;width: 300px" class="form-control">

                                </select>
                            </td>
                            <td style="width:450px"><label style="float: left;margin-top: 10px">购买日期：</label><input
                                    type="date" id="datett" style="margin-top: 10px;width: 350px" class="form-control">
                            </td>
                        </tr>
                        <tr style="margin: 20px; margin-left: 20px;padding: 20px;">
                            <td style="width:400px;padding-left: 20px"><label
                                    style="float: left;margin-top: 10px">项目名称：</label>
                                <select style="margin-top: 10px;width: 300px" id="subject" class="form-control">

                                </select>
                            </td>
                            <td style="width:500px"><label style="float: left;margin-top: 10px">单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                价：</label><input type="text" id="dj" readonly
                                                 style="float: left;margin-top: 10px;margin-right:20px;width: 125px"
                                                 class="form-control"><label style="float: left;margin-top: 10px">数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;量：</label><input
                                    type="text" id="sl" value="1" style="float: left;margin-top: 10px;width: 125px"
                                    oninput="slChange()" class="form-control"></td>
                        </tr>
                        <tr style="margin: 20px; margin-left: 20px;padding: 20px;">
                            <td colspan="2" style="width:900px;padding-left: 20px"><label
                                    style="float: left;margin-top: 10px">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                注：</label>
                                <input type="text" id="bz" style="margin-top: 10px;width: 740px" class="form-control">
                            </td>
                        </tr>
                        <tr style="margin: 20px; margin-left: 20px;padding: 20px;">
                            <td colspan="2" style="width:900px;padding-left: 20px"><input type="text"
                                                                                          style="float:right;margin-top: 10px;margin-right:30px;width: 100px"
                                                                                          id="zje" class="form-control"
                                                                                          readonly="readonly"><label
                                    style="float: right;margin-top: 10px">总金额：</label>

                            </td>
                        </tr>
                        <tr style="margin: 20px; margin-left: 20px;padding: 20px;">
                            <td colspan="2" style="width:900px;padding-left: 20px"><input type="hidden"
                                                                                          style="float:right;margin-top: 10px;margin-right:30px;width: 100px"
                                                                                          class="form-control"
                                                                                          readonly="readonly"><label
                                    style="float: right;margin-top: 10px"></label>

                            </td>
                        </tr>
                    </table>
                </form>
                &nbsp;
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button onclick="save()" id="add" type="button" class="btn btn-primary">确认报课</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal3">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- form开始 -->
                <table id="tb"></table>
                &nbsp;
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal2">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel2">❤变更教师</h4>
            </div>
            <div class="modal-body">
                <!-- form开始 -->
                <form>
                    <input type="hidden" id="id" name="id">
                    <table style="width: 90%;margin: 0px auto">
                        <tr style="width: 100%">
                            <td style="width: 280px;height: 35px;">会员卡号：<span id="hykh"></span></td>
                            <td style="width:280px;height: 35px;">会员姓名：<span id="hyxm"></span></td>
                        </tr>
                        <tr style="width: 100%">
                            <td style="width: 280px;height: 35px;">原来老师：<span id="yssj"></span></td>
                            <td style="width: 280px;height: 35px;">变更老师：
                                <select style="width: 140px;float: right;margin-right: 30px" id="xgcoach"
                                        class="form-control">
                                    <option value="-1">--请选择--</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </form>
                &nbsp;
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button onclick="upd1()" type="button" class="btn btn-primary">修改</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
